package com.fht.controller.food;


import com.fht.entity.*;
import com.fht.from.FoodCategoryFrom;
import com.fht.service.food.FoodMaterialCategoryService;
import com.fht.service.food.FoodMaterialManagerService;
import com.fht.service.food.MerchantsInformationService;
import com.fht.util.ResultVOUtil;
import com.fht.vo.LayUITableVO;
import com.fht.vo.ResultVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@Slf4j
@RequestMapping("/food/material")
public class foodMaterialController {


    @Autowired
    public FoodMaterialManagerService foodMaterialManagerService;

    @Autowired
    public FoodMaterialCategoryService foodMaterialCategoryService;

    @Autowired
    public MerchantsInformationService merchantsInformationService;

    //展示
    @GetMapping("/index")
    public String index(Model model){

        List<MerchantsInformation> merchantsInformationList = merchantsInformationService.findAllIdAndName();
        model.addAttribute("merchantsInformationList",merchantsInformationList);

        return "food/foodMaterial/index";

    }

    //获取菜品类别信息
    @GetMapping("/list")
    @ResponseBody
    public LayUITableVO<List<FoodMaterialManager>> list (@RequestParam(defaultValue = "1") Integer page,
                                                         @RequestParam(defaultValue = "10") Integer limit,
                                                         FoodCategoryFrom from){
        Map<String ,Object> map = new HashMap<>();
        map.put("page",page);
        map.put("limit",limit);
        map.put("from",from);

        LayUITableVO<List<FoodMaterialManager>> vo = foodMaterialManagerService.findPage(map);


        return vo;
    }


    //修改菜品类别信息1（查询指定id菜品分类详情）
    @GetMapping("/edit-ui")
    public String editUI(String id,String merchantsId,Model model){

        FoodMaterialManager foodMaterial = foodMaterialManagerService.selectById(id);

        List<FoodMaterialCategory> categorys = foodMaterialCategoryService.selectBymerchantsId(merchantsId);

        List<FoodMaterialCategory> units = foodMaterialCategoryService.selectAllUnit();

        model.addAttribute("categorys",categorys);
        model.addAttribute("units",units);
        model.addAttribute("foodMaterial",foodMaterial);
        model.addAttribute("id",id);

        return "food/foodMaterial/edit";

    }
    //执行修改
    @PostMapping("/edit")
    @ResponseBody
    public ResultVO edit(FoodMaterialManager foodMaterialManager){

        foodMaterialManagerService.update(foodMaterialManager);

        return ResultVOUtil.ok();
    }

    //执行删除
    @PostMapping("/del")
    @ResponseBody
    public ResultVO del(String id){
        foodMaterialManagerService.delelteById(id);
        return ResultVOUtil.ok();
    }

    //跳转至添加页面
    @GetMapping("/add-ui")
    public String addUI(String merchantsId,Model model){

        List<FoodMaterialCategory> categorys = foodMaterialCategoryService.selectBymerchantsId(merchantsId);

        List<FoodMaterialCategory> units = foodMaterialCategoryService.selectAllUnit();

        model.addAttribute("categorys",categorys);
        model.addAttribute("units",units);
        model.addAttribute("merchantsId",merchantsId);
        return "food/foodMaterial/add";
    }

    //执行添加
    @PostMapping("/add")
    @ResponseBody
    public ResultVO add(FoodMaterialManager foodMaterialManager){

        foodMaterialManagerService.add(foodMaterialManager);

        return ResultVOUtil.ok();
    }

    //跳转类别管理页面
    @GetMapping("/editCategory-ui")
    public String editCategoryUI(String merchantsId,Model model){


        model.addAttribute("merchantsId",merchantsId);
        return "food/foodMaterial/categoryEdit";
    }

    //查询类别信息
    @GetMapping("/categoryList")
    @ResponseBody
    public LayUITableVO<List<FoodMaterialCategory>> categoryList (@RequestParam(defaultValue = "1") Integer page,
                                                         @RequestParam(defaultValue = "10") Integer limit,
                                                         @RequestParam(defaultValue = "0") String merchantsId){


        List<FoodMaterialCategory> foodMaterialCategories = foodMaterialCategoryService.selectBymerchantsId(merchantsId);
        LayUITableVO<List<FoodMaterialCategory>> vo = new LayUITableVO<>();
        vo.setData(foodMaterialCategories);


        return vo;
    }
    //执行添加
    @PostMapping("/addCategory")
    @ResponseBody
    public ResultVO addCategory(FoodMaterialCategory foodMaterialCategory){

        foodMaterialCategoryService.addCategory(foodMaterialCategory);

        return ResultVOUtil.ok();
    }


    //执行修改
    @PostMapping("/editCategory")
    @ResponseBody
    public ResultVO editCategory(FoodMaterialCategory foodMaterialCategory){

        foodMaterialCategoryService.update(foodMaterialCategory);

        return ResultVOUtil.ok();
    }


    //执行删除
    @PostMapping("/delCategory")
    @ResponseBody
    public ResultVO delCategory(String id){
        foodMaterialCategoryService.delelteById(id);
        return ResultVOUtil.ok();
    }


    //跳转类别管理页面
    @GetMapping("/editUnit-ui")
    public String editUnitUI(){


        return "food/foodMaterial/unitEdit";
    }

    //修改菜品类别信息1（查询指定id菜品分类详情）
    @GetMapping("/editRepertory-ui")
    public String editRepertoryUI(String id,Model model){

        FoodMaterialManager foodMaterialManager = foodMaterialManagerService.selectById(id);


        model.addAttribute("foodMaterialManager",foodMaterialManager);


        return "food/foodMaterial/editRepertory";

    }

    //执行修改
    @PostMapping("/editRepertory")
    @ResponseBody
    public ResultVO editRepertory(FoodMaterialManager foodMaterialManager){

        foodMaterialManagerService.updateRepertory(foodMaterialManager);



        return ResultVOUtil.ok();
    }

}
